package jp.co.cos.ittrick.channel.controller.admin;

import jp.co.cos.ittrick.channel.model.CategoryModel;
import jp.co.cos.ittrick.channel.model.ChannelModel;
import jp.co.cos.ittrick.channel.service.CategoryService;
import jp.co.cos.ittrick.channel.service.ChannelService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

public class CategoryInsertController extends Controller {

    CategoryService categoryService;
    ChannelService channelService;

    public CategoryInsertController() {
        categoryService = new CategoryService();
        channelService = new ChannelService();
    }

    @Override
    public Navigation run() throws Exception {

        // リクエストパラメータ
        String channel = asString("channel");
        String name = asString("name");
        String index = asString("index");

        ChannelModel channelModel = channelService.getChannelByKey(channel);

        // Modelの設定
        CategoryModel model = new CategoryModel();
        model.setName(name);
        model.setIndex(Integer.valueOf(index));
        model.getChannelModelRef().setModel(channelModel);

        // Insert
        categoryService.insert(model);

        return redirect("/admin/channel?channel=" + channel);
    }
}
